<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Frog.NET</title>
        
        <link rel="Stylesheet" href="style.css" type="text/css" />
    </head>

    <body>
        <table class="navbar">
            <tr>
                <td>Frog.NET</td>
                <td align="right">v0.3</td>
            </tr>
        </table>
        
        <div id="logoContainer"><img src="frog.jpg" id="logo" /></div>
        
        <br />
        <a href="index.html">Home</a>
        
        <div id="mainText">
        <h1>Getting Started</h1>
        
        <a name="CreateMappingClass" />
        <h3>Creating a new mapping class</h3>        
        <ol type="1">
            <li>Copy the Frog.Orm.dll assembly to the Lib directory of your project.</li>
            <li>Add a reference to the assembly.</li>
            <li>Next, add a new empty class.</li>
            <li>Add public properties to the class, that matches your database schema (column<br />
             types and names) like in the example below.</li>
        </ol>
        
        <!-- code formatted by http://manoli.net/csharpformat/ -->
        <div class="csharpcode">
        <pre><span class="lnum">   1:  </span>[Table]</pre>
        <pre><span class="lnum">   2:  </span><span class="kwrd">public</span> <span class="kwrd">class</span> User</pre>
        <pre><span class="lnum">   3:  </span>{</pre>
        <pre><span class="lnum">   4:  </span>     [PrimaryKey]</pre>
        <pre><span class="lnum">   5:  </span>     <span class="kwrd">public</span> <span class="kwrd">long</span> Id { get; set; }</pre>
        <pre><span class="lnum">   6:  </span>     </pre>
        <pre><span class="lnum">   7:  </span>     [Column]</pre>
        <pre><span class="lnum">   8:  </span>     <span class="kwrd">public</span> <span class="kwrd">string</span> Name { get; set; }</pre>
        <pre><span class="lnum">   9:  </span>}</pre>
        </div>    
        
        <a name="Retrieving" />
        <h3>Retrieving data from the database</h3>
        In order to retrieve data you first <a href="#CreateMappingClass">create a mapping class</a>, that matches the structure of the data you are trying to fetch.
        Next, you can instantiate a Repository object that will actually fetch the data. See example below.

        <!-- code formatted by http://manoli.net/csharpformat/ -->
        <div class="csharpcode">
        <pre><span class="lnum">   1:  </span><span class="kwrd">public</span> <span class="kwrd">void</span> Test()</pre>
        <pre><span class="lnum">   2:  </span>{</pre>
        <pre><span class="lnum">   3:  </span>    <span class="kwrd">using</span>(var connection = <span class="kwrd">new</span> Connection(<span class="str">"&lt;connection string&gt;"</span>);)</pre>
        <pre><span class="lnum">   4:  </span>    {</pre>
        <pre><span class="lnum">   5:  </span>        var repository = <span class="kwrd">new</span> Repository(connection)</pre>
        <pre><span class="lnum">   6:  </span>        var users = repository.GetAll&lt;User&gt;();</pre>
        <pre><span class="lnum">   7:  </span>        </pre>
        <pre><span class="lnum">   8:  </span>        <span class="kwrd">foreach</span>(var user <span class="kwrd">in</span> users)</pre>
        <pre><span class="lnum">   9:  </span>        {</pre>
        <pre><span class="lnum">  10:  </span>            Console.WriteLine(user.Name);</pre>
        <pre><span class="lnum">  11:  </span>        }</pre>
        <pre><span class="lnum">  12:  </span>    }</pre>
        <pre><span class="lnum">  13:  </span>}</pre>
        </div>
        
        In the example above, the Repository object returns a set of users when GetAll&lt;User&gt; is called. The set is then iterated and the name og each user 
        printed to the console.<br />
        <br />
        Naturally you can also perform more complicated queries using a repository. For instance you also want to get the users that meet a certain criteria. See the example below.
        
        <!-- code formatted by http://manoli.net/csharpformat/ -->
        <div class="csharpcode">
        <pre><span class="lnum">   1:  </span><span class="kwrd">public</span> <span class="kwrd">void</span> Test()</pre>
        <pre><span class="lnum">   2:  </span>{</pre>
        <pre><span class="lnum">   3:  </span>    <span class="kwrd">using</span>(var connection = <span class="kwrd">new</span> Connection(<span class="str">"&lt;connection string&gt;"</span>);)</pre>
        <pre><span class="lnum">   4:  </span>    {</pre>
        <pre><span class="lnum">   5:  </span>        var repository = <span class="kwrd">new</span> Repository(connection)</pre>
        <pre><span class="lnum">   6:  </span>        var users = repository.GetWhere&lt;User&gt;(Field.And(</pre>
        <pre><span class="lnum">   7:  </span>                                                Field.Equals("Name", "John"),</pre>
        <pre><span class="lnum">   8:  </span>                                                Field.GreaterThan("Age", 45)</pre>
        <pre><span class="lnum">   9:  </span>                                                )</pre>
        <pre><span class="lnum">  10:  </span>                                             );</pre>
        <pre><span class="lnum">  11:  </span>        </pre>
        <pre><span class="lnum">  12:  </span>        <span class="kwrd">foreach</span>(var user <span class="kwrd">in</span> users)</pre>
        <pre><span class="lnum">  13:  </span>        {</pre>
        <pre><span class="lnum">  14:  </span>            Console.WriteLine(user.Name);</pre>
        <pre><span class="lnum">  15:  </span>        }</pre>
        <pre><span class="lnum">  16:  </span>    }</pre>
        <pre><span class="lnum">  17:  </span>}</pre>
        </div>

        The example fetches all users named John that has an age above 45. See more about conditions in <a href="Conditions.htm">Conditions</a>.
        
        Note that the Get methods on the repository returns IEnumerables, which means you cannot iterate them twice - unless you convert them to Lists (see .ToList())
        
        <a name="Storing" />
        <h3>Storing data in the database</h3>
        As with retrieving data, you need a mapping class to store data in the database. In most situations you can use the same mapping class that is used when fetching data.
        You can create a new instance of your object, and provide it as a parameter to the Create or Update method on a Repository object. See example below.
        
        <!-- code formatted by http://manoli.net/csharpformat/ -->
        <div class="csharpcode">
        <pre><span class="lnum">   1:  </span><span class="kwrd">public</span> <span class="kwrd">void</span> Test()</pre>
        <pre><span class="lnum">   2:  </span>{</pre>
        <pre><span class="lnum">   3:  </span>    <span class="kwrd">using</span>(var connection = <span class="kwrd">new</span> Connection(<span class="str">"&lt;connection string&gt;"</span>))</pre>
        <pre><span class="lnum">   4:  </span>    {</pre>
        <pre><span class="lnum">   5:  </span>        var repository = <span class="kwrd">new</span> Repository(connection)</pre>
        <pre><span class="lnum">   6:  </span>        var user =  <span class="kwrd">new</span> User();</pre>
        <pre><span class="lnum">   7:  </span>        user.Name = <span class="str">"Bill Gates"</span>;</pre>
        <pre><span class="lnum">   8:  </span>        </pre>
        <pre><span class="lnum">   9:  </span>        user = repository.Create(user);</pre>
        <pre><span class="lnum">  10:  </span>        </pre>
        <pre><span class="lnum">  11:  </span>        connection.CommitChanges();</pre>
        <pre><span class="lnum">  12:  </span>    }</pre>
        <pre><span class="lnum">  13:  </span>}</pre>
        </div>
        
        In the example above a new instance of type User is constructed and the name is then set. Subsequently the repository is asked to create this user in the 
        database, and finally actually commit the changes. If you do not explicitly call the CommitChanges() method, the changes will be rolled back when the 
        repository is disposed.
        
        </div>
        <div class="footer">
        Frog.NET 
        </div>
    </body>
</html>
